---
--类名: DrawLayer.lua
--创建日期: 2016-1-29
--创建人: 张翼
--功能描述: 
--修改内容:
--修改日期:   
--修改人:      
--修改备注: 
--


local testData={
     {
         p = {
             x = 391,
             y = 390.99996948242
         },
         t = 0.0994
     },
     {
         p = {
             x = 264.99996948242,
             y = 436.99996948242
         },
         t = 1.2164
     },
     {
         p = {
             x = 266.99996948242,
             y = 420.99996948242
         },
         t = 1.2835
     },
     {
         p = {
             x = 272,
             y = 399.99996948242
         },
         t = 1.2995
     },
     {
         p = {
             x = 277,
             y = 377.99996948242
         },
         t = 1.3165
     },
     {
         p = {
             x = 283.99996948242,
             y = 357.99996948242
         },
         t = 1.3335
     },
     {
         p = {
             x = 290,
             y = 342.99996948242
         },
         t = 1.3485
     },
     {
         p = {
             x = 298.99996948242,
             y = 328.99996948242
         },
         t = 1.3655
     },
     {
         p = {
             x = 305,
             y = 316.99996948242
         },
         t = 1.3825
     },
     {
         p = {
             x = 309,
             y = 311.99996948242
         },
         t = 1.3845
     },
     {
         p = {
             x = 311,
             y = 307.99996948242
         },
         t = 1.3995
     },
     {
         p = {
             x = 315,
             y = 304
         },
         t = 1.4025
     },
     {
         p = {
             x = 318.99996948242,
             y = 299
         },
         t = 1.4165
     },
     {
         p = {
             x = 324,
             y = 294.99996948242
         },
         t = 1.4175
     },
     {
         p = {
             x = 335,
             y = 287.99996948242
         },
         t = 1.4325
     },
     {
         p = {
             x = 346,
             y = 281.99996948242
         },
         t = 1.4485
     },
     {
         p = {
             x = 354,
             y = 279
         },
         t = 1.4665
     },
     {
         p = {
             x = 361.99996948242,
             y = 275.99996948242
         },
         t = 1.4825
     },
     {
         p = {
             x = 367,
             y = 274
         },
         t = 1.4985
     },
     {
         p = {
             x = 373.99996948242,
             y = 271.99996948242
         },
         t = 1.5155
     },
     {
         p = {
             x = 382.99996948242,
             y = 270.99996948242
         },
         t = 1.5325
     },
     {
         p = {
             x = 397,
             y = 267.99996948242
         },
         t = 1.5485
     },
     {
         p = {
             x = 410.99996948242,
             y = 264.99996948242
         },
         t = 1.5655
     },
     {
         p = {
             x = 425.99996948242,
             y = 262.99996948242
         },
         t = 1.5825
     }
 }
 






local DrawLayer = class("DrawLayer",function()
	return display.newLayer()
end)

DrawLayer.NEAR_DIS=0  --x像素内不计数 0则全部计数
--工作状态
DrawLayer.STATES={
    Ready=0,
    Drawing=1,
    Playing=2
}

function DrawLayer:ctor()
    --上次触摸点
    self.lastPoint=nil
    --存储画点信息列表
    self.drawInfos={}
    --默认半径5像素
    self.radius=5
    --默认颜色
    self.color=cc.c4b(1,0,0,1)
    --默认涂鸦时间5秒
    self.drawTime=5
    --开始涂鸦时间
    self.startDrawTime=app:getTime()
    --工作状态
    self.state=DrawLayer.STATES.Ready
    --开始播放时间
    self.startPlayingTime=app:getTime()
    --播放标记
    self.playingFlag=0
    --播放数据
    self.playingData=testData

    --print("PlayerController:ctor0 time"..app:getTime())

    self:addTouchMask()
    self:draw()


    self.timer=cc.Director:getInstance():getScheduler():scheduleScriptFunc(function()self:tick()end,0,false)

    --开始画
    self:startDraw()

    --开始播放
    --self:startPlaying()


end


function DrawLayer:tick()
    --播放状态
    if self.state==DrawLayer.STATES.Playing and self.playingFlag>0 then
        --已经过去的时间
        local ct=app:getTime()-self.startPlayingTime
        --当前动画时间
        local t=self.playingData[self.playingFlag].t
        local p=cc.p(self.playingData[self.playingFlag].x*display.widthInPixels,self.playingData[self.playingFlag].y*display.heightInPixels)
        if t<ct then
            --时间已过，可以播放
            if self:isOutPoint(p) then
                --远点，补线
                self:createDrawLine(self.lastPoint,p)
            else
                self:createDrawNode(p)
            end
            self:createDrawNode(p)
            self.lastPoint=p
            if self.playingFlag+1<=#self.playingData then
                self.playingFlag=self.playingFlag+1
            else
                self.playingFlag=0
            end
        end
    end
end

--开始画
function DrawLayer:startDraw()
   
    self.state=DrawLayer.STATES.Drawing

    self.startDrawTime=app:getTime()
    
    performWithDelay(self,function()  
        self:endDraw()
    end,self.drawTime) 
end

--结束画
function DrawLayer:endDraw()
    self.state=DrawLayer.STATES.Ready
    self:removeAllChildren()
    print("endDraw")
    dump(self.drawInfos)
end


--开始播放
function DrawLayer:startPlaying()

    self.state=DrawLayer.STATES.Playing
    self.playingFlag=1
    self.startPlayingTime=app:getTime()

    performWithDelay(self,function()  
        self:endPlaying()
    end,self.drawTime) 
end

--结束播放
function DrawLayer:endPlaying()

    self.playingFlag=0
    self.playingData={}

    self.state=DrawLayer.STATES.Ready
    self:removeAllChildren()
    print("endPlaying")
    dump(self.drawInfos)
end

--补线
function DrawLayer:createDrawLine(p1,p2)
    local drawNode = cc.NVGDrawNode:create()  
    drawNode:setLineWidth(self.radius*2)
    self:addChild(drawNode,999)
    drawNode:drawLine(p1,p2,self.color)
end

--画点
function DrawLayer:createDrawNode(p)
    local drawNode = cc.NVGDrawNode:create()  
    self:addChild(drawNode,999)
    drawNode:drawDot(p,self.radius,self.color)
end

function DrawLayer:getDistance(p1,p2)
    return math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y))
end

--是否近点
function DrawLayer:isNearPoint(p)
    if self.lastPoint then
        local dis=self:getDistance(p,self.lastPoint)
        if dis<=DrawLayer.NEAR_DIS then
            return true
        else
            return false
        end
    end
    return false
end

--是否远点（需要补线）
function DrawLayer:isOutPoint(p)
    if self.lastPoint then
        local dis=self:getDistance(p,self.lastPoint)
        if dis>self.radius*2 then
            return true
        else
            return false
        end
    end
    return false
end

function DrawLayer:addTouchPoint(touch,event)
    local p=touch:getLocation()
    print("posX"..p.x.."posY"..p.y)
    if self:isNearPoint(p) then
        --近点重复画，忽略
        return
    end
    if self:isOutPoint(p) then
        --远点，补线
        self:createDrawLine(self.lastPoint,p)
    else
        self:createDrawNode(p)
    end
    local drawInfo={p=cc.p(math.modf(p.x/display.widthInPixels,5),math.modf(p.x/display.heightInPixels,5)),t=app:getTime()-self.startDrawTime}
    table.insert(self.drawInfos,drawInfo)
    self.lastPoint=p
end

function DrawLayer:addTouchMask()

    
    local function onTouchBegan(touch, event)
        self:addTouchPoint(touch,event)
        return true
    end

    local function onTouchMoved(touch, event)
        self:addTouchPoint(touch,event)
    end

    local function onTouchEnded(touch, event)
        --self:addTouchPoint(touch,event)
        print("onTouchEnded")
    end

    local listener1 = cc.EventListenerTouchOneByOne:create()
    --listener1:setSwallowTouches(false)
    listener1:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
    listener1:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )
    listener1:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED )
    local eventDispatcher = self:getEventDispatcher()
    eventDispatcher:addEventListenerWithSceneGraphPriority(listener1, self)
end

return DrawLayer